import config from '@/config/config.js';
import http from '@/api/http.js'

let unitRequest={
    loadUnits(unit,page=1,limit=10,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/units/list?page="+page + "&limit=" + limit;
            if(unit){
                if(unit.unitCode){
                    url += "&unitCode=" + unit.unitCode;
                }
                if(unit.unitName){
                    url += "&unitName=" + unit.unitName;
                }
            }
            http.get(url,callBack);
        }else{
            callBack(undefined);
        }
    },save(unit,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/units/save";
            http.post(url,unit,callBack);
        }else{
            callBack(undefined);
        }
    }, updateUnitById(unit,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/units/update";
            http.put(url,unit,callBack);
        } else {
            callBack(undefined);
        }
    }, selectById(unitId,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/units/selectById?unitId=" + unitId;
            http.get(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deleteUnitById(unitId,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/units/delete?unitId=" + unitId;
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deletesUnit(unitIds,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/units/deletes?a=1";
            if(unitIds){
                unitIds.forEach(id=> url += '&unitId='+id);
            }
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    }, ListUnits(callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/units/listUnits";
            http.get(url,callBack);
        } else {
            callBack(undefined);
        }
    }
}

export default unitRequest;